# The Long-Term Impact of Mobilization and Repression on Political Trust
# R Code for Replication
# By: Scott Desposato, Gang Wang, Jason Wu
# 
# This version: 22 December 2020
# R version 4.0.2 (2020-06-22)
#
###########################################################################
###                                                                     ###
###                      LOADING PACKAGES AND DATA                      ###
###                                                                     ###
###########################################################################
### Clear Global Environment ###
rm(list=ls())

### Loading Packages ###
library(sem)         # sem_3.1.11
library(MASS)        # MASS_7.3.51.6
library(AER)         # AER_1.2.9 

### Loading Data ###
load("DWW_CPS.RData")



##########################################################################
###                                                                    ###
###                 MAIN TEXT ANALYSIS AND RESULTS                     ###
###                                                                    ###
##########################################################################

##########################################################################
##  Table 2: Survey Methods and Number of Observations                  ##
##########################################################################
## Legend ##
# 1 = Email
# 2 = Online
# 3 = Face to Face
# 4 = Telephone
table(data$method)
# Total
sum(table(data$method))



##########################################################################
##  Table 3: Summary Statistics                                         ##
##########################################################################
### Control and Treatment Summarty Statistics ###
### Controls are columns labeled  "0" ###
### Treatment are columns labeled "1" ###
## Age Entered College (Mean) ##
# Control #
mean(data$Age_Enter_college [data$T == 0], na.rm = TRUE)
# Treatment #
mean(data$Age_Enter_college [data$T == 1], na.rm = TRUE)

# Where did R Grow Up (%)
prop.table(table(data$V01a, data$T), margin = 2)

# Gender (%)
prop.table(table(data$Female, data$T), margin = 2)

# Marital Status (%)
prop.table(table(data$V23, data$T), margin=2)

# Education (%)
prop.table(table(data$V4a, data$T),margin=2)

# Income in RMB (%)
prop.table(table(data$Income, data$T), margin=2)

# N
Ntable <- table(data$T)
Ntable      # 0 = Control | 1 = Treatment
sum(Ntable) # Overall N

## Overall ##
# Age Entered College (Mean)
mean(data$Age_Enter_college, na.rm = TRUE)

# Where did R Grow Up (%)
prop.table(margin.table(table(data$V01a, data$T), margin=1))

# Gender (%)
prop.table(table(data$Female, data$T), margin = 2)

# Marital Status (%) 0 = Control 1 = Treatment
prop.table(table(data$V23, (data$T == 0 | data$T == 1)), margin=2)

# Education (%)
prop.table(margin.table(table(data$V4a, data$T),margin=1))

# Income in RMB (%)
prop.table(margin.table(table(data$Income, data$T),margin=1))

# N
Ntable <- table(data$T)
Ntable      # 0 = Control 1 = Treatment
sum(Ntable) # Overall N



##########################################################################
##  Table 4: Simple First Stage Models for Fuzzy RDD                    ##
##########################################################################
### Cohort Years Included: 1988 - 1989
## Subsetting the Data
temp <- subset(x = data, year_88_89 == 1)
## Simply Fuzzy RDD
ivmodel1 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel1)

### Cohort Years Included: 1987 - 1990
## Subsetting the Data
temp <- subset(x = data, year_87_90 == 1)
## Simply Fuzzy RDD
ivmodel2 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel2)

### Cohort Years Included: 1986 - 1991
## Subsetting the Data
temp <- subset(x = data, year_86_91 == 1)
## Simply Fuzzy RDD
ivmodel3 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel3)

### Cohort Years Included: 1985 - 1992
## Subsetting the Data
temp <- subset(x = data, year_85_92 == 1)
## Simply Fuzzy RDD
ivmodel4 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel4)

### Cohort Years Included: 1984 - 1993
## Subsetting the Data
temp <- subset(x = data, year_84_93 == 1)
## Simply Fuzzy RDD
ivmodel5 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel5)

### Cohort Years Included: 1984 - 1994
## Subsetting the Data
temp <- subset(x = data, year_84_94 == 1)
## Simply Fuzzy RDD
ivmodel6 <- ivreg(T ~ D + X_c, data = temp)
summary(ivmodel6)



##########################################################################
##  Table 5: Trust in Central Government is Significantly Lower for     ##
##  Tiananmen Cohort                                                    ##
##########################################################################
### Central Government
## Model 1: Ordinarly Least Squares (OLS) ##
M1 <- lm(TrustCent ~ T, data = data)
M1$coefficients[2]
summary(M1)

## Model 2: Ordinarly Least Squares (OLS) ##
M2 <- lm(TrustCent ~ T + Education + CCP + GovtJob + Income2 + marriage, data = data)
M2$coefficients[2]
summary(M2)

## Model 3: Baseline Fuzzy RDD ##
M3 <- ivreg(TrustCent ~ T |D+X_c, data = data)
M3$coefficients[2]
summary(M3)

## Model 4: Fuzzy RDD with Covariates ##
M4 <- ivreg(TrustCent ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c + Education + CCP + GovtJob + Income2 + marriage, data = data)
M4$coefficients[2]
summary(M4)

## Model 5: Complete Baseline Fuzzy RDD ##
M5 <- ivreg(TrustCent ~ T |D+X_c+X2+X3+DX+DX2+DX3, data = data)
M5$coefficients[2]
summary(M5)

## Model 6: Complete Fuzzy RDD with Covariates ##
M6 <- ivreg(TrustCent ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c+X2+X3+DX+DX2+DX3 + Education + CCP + GovtJob + Income2 + marriage, data = data)
M6$coefficients[2]
summary(M6)

### Provincial Government
## Model 1: Ordinarly Least Squares (OLS) ##
M1 <- lm(TrustProv ~ T, data = data)
M1$coefficients[2]
summary(M1)

## Model 2: Ordinarly Least Squares (OLS) ##
M2 <- lm(TrustProv ~ T + Education + CCP + GovtJob + Income2 + marriage, data = data)
M2$coefficients[2]
summary(M2)

## Model 3: Baseline Fuzzy RDD ##
M3 <- ivreg(TrustProv ~ T |D+X_c, data = data)
M3$coefficients[2]
summary(M3)

## Model 4: Fuzzy RDD with Covariates ##
M4 <- ivreg(TrustProv ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c + Education + CCP + GovtJob + Income2 + marriage, data = data)
M4$coefficients[2]
summary(M4)

## Model 5: Complete Baseline Fuzzy RDD ##
M5 <- ivreg(TrustProv ~ T |D+X_c+X2+X3+DX+DX2+DX3, data = data)
M5$coefficients[2]
summary(M5)

## Model 6: Complete Fuzzy RDD with Covariates ##
M6 <- ivreg(TrustProv ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c+X2+X3+DX+DX2+DX3 + Education + CCP + GovtJob + Income2 + marriage, data = data)
M6$coefficients[2]
summary(M6)

### Local Government
## Model 1: Ordinarly Least Squares (OLS) ##
M1 <- lm(TrustLocal ~ T, data = data)
M1$coefficients[2]
summary(M1)

## Model 2: Ordinarly Least Squares (OLS) ##
M2 <- lm(TrustLocal ~ T + Education + CCP + GovtJob + Income2 + marriage, data = data)
M2$coefficients[2]
summary(M2)

## Model 3: Baseline Fuzzy RDD ##
M3 <- ivreg(TrustLocal ~ T |D+X_c, data = data)
M3$coefficients[2]
summary(M3)

## Model 4: Fuzzy RDD with Covariates ##
M4 <- ivreg(TrustLocal ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c + Education + CCP + GovtJob + Income2 + marriage, data = data)
M4$coefficients[2]
summary(M4)

## Model 5: Complete Baseline Fuzzy RDD ##
M5 <- ivreg(TrustLocal ~ T |D+X_c+X2+X3+DX+DX2+DX3, data = data)
M5$coefficients[2]
summary(M5)

## Model 6: Complete Fuzzy RDD with Covariates ##
M6 <- ivreg(TrustLocal ~ T + Education + CCP + GovtJob + Income2 + marriage
            |D+X_c+X2+X3+DX+DX2+DX3 + Education + CCP + GovtJob + Income2 + marriage, data = data)
M6$coefficients[2]
summary(M6)



